package com.longshine.downcomp;

import java.util.HashMap;
import java.util.Map;

import com.longshine.platform.equip.tcp.ProtocolDispacher;
import com.longshine.platform.equip.tcp.ProtocolUtil;
import com.longshine.tools.util.NumberUtil;

public class RobotSystem extends EquipSystem{

	protected Map<Integer,Boolean> turnState ;
	
	
	public RobotSystem(int systemType, int downNo) {
		super(systemType, downNo);
		turnState = new HashMap<Integer,Boolean>();
		turnState.put(1, true);
		turnState.put(2, false);
		status = 41;
	}
	@Override
	public void recvCommand(int taskId, int functionCode, String dataArea)
			throws Exception {
		if(functionCode == ProtocolDispacher.FUNCTION_TRANSIT_QUERY){
			String rdataArea = "000000";
			rdataArea += turnState.get(2)?1:0;
			rdataArea += turnState.get(1)?1:0;
			rdataArea = NumberUtil.binary2Hex(rdataArea, 1);
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType, functionCode, rdataArea));
			return;
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_LOAD_READY){
			turnState.put(1, false);
			turnState.put(2, true);
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType, ProtocolDispacher.FUNCTION_REPLY, ProtocolDispacher.DATAAREA_COMFIRM));
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_START_STOP){
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType, ProtocolDispacher.FUNCTION_REPLY, ProtocolDispacher.DATAAREA_COMFIRM));
			//上报机器人状态
			dataArea = NumberUtil.hex2Binary(dataArea);
			if(dataArea.charAt(dataArea.length()-1)=='1'){
				status = 44;
			}
			else{
				status = 41;
			}
			sendStatusComm();
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_STATE_QUERY){
			conn.sendStr(ProtocolUtil.createProtocol(downNo,taskId,systemType,ProtocolDispacher.FUNCTION_STATE_QUERY,NumberUtil.toHexStr(status, 1)));
			return;
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_GRIP_BATTERY
				|| functionCode == ProtocolDispacher.FUNCTION_TAKEOUT_BATTERY
				|| functionCode == ProtocolDispacher.FUNCTION_LOAD_POSITION
				|| functionCode == ProtocolDispacher.FUNCTION_LOAD_BATTERY
		){
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType,
					ProtocolDispacher.FUNCTION_REPLY,
					ProtocolDispacher.DATAAREA_COMFIRM));
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_CHANGE_TRNASIT_BATBOX){
			turnState.put(1, true);
			turnState.put(2, false);
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType,
					ProtocolDispacher.FUNCTION_REPLY,
					ProtocolDispacher.DATAAREA_COMFIRM));
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_FORCE_STOP){
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType,
					ProtocolDispacher.FUNCTION_REPLY,
					ProtocolDispacher.DATAAREA_COMFIRM));
			status = 41;
			sendStatusComm();
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_RESET){
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType,
					ProtocolDispacher.FUNCTION_REPLY,
					ProtocolDispacher.DATAAREA_COMFIRM));
		}
		else if(functionCode == ProtocolDispacher.FUNCTION_PAUSE_RESUME){
			conn.sendStr(ProtocolUtil.createProtocol(downNo, taskId, systemType,
					ProtocolDispacher.FUNCTION_REPLY,
					ProtocolDispacher.DATAAREA_COMFIRM));
			int action = NumberUtil.hex2Decimal(dataArea);
			if(action == 1){
				status = 44;
			}
			else{
				status = 43;
			}
			sendStatusComm();
		}
	}

	
	public void sendStatusComm() throws Exception{
		String dataArea = NumberUtil.toHexStr(status, 1);
		conn.sendStr(ProtocolUtil.createProtocol(downNo,0,systemType,ProtocolDispacher.FUNCTION_STATE_UPLOAD,dataArea));
	}
	
	public static void main(String[] args) throws Exception{
//		System.out.println(NumberUtil.binary2Hex("00000001", 1));
		new RobotSystem(2,2).startWork();
	}


}
